﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://wpfimageviewer.codeplex.com
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using BrainTechLLC;
using AForge.Imaging.Filters;
using System.Drawing;

namespace BrainTechLLC.ImageOperators
{
    public static class Util3
    {
        public static IFilter CreateFilter(this Type type)
        {
            var constructor = type.GetConstructor(Type.EmptyTypes);
            if (constructor != null)
            {
                IFilter filter = constructor.Invoke(null) as IFilter;
                return filter;
            }
            return null;
        }

        public static long GetByteEstimate(this Bitmap vm)
        {
            return GetByteEstimate(vm, vm.Width, vm.Height, vm.PixelFormat);
        }

        public static long GetByteEstimate(this Bitmap vm, int width, int height, System.Drawing.Imaging.PixelFormat pixelFormat)
        {
            long bytesTaken = width * height;
            var s = pixelFormat.ToString();
            if (s.Contains("Format16")) bytesTaken *= 2;
            else if (s.Contains("Format32")) bytesTaken *= 4;
            else if (s.Contains("Format24")) bytesTaken *= 3;
            else if (s.Contains("Format48")) bytesTaken *= 6;
            else if (s.Contains("Format64")) bytesTaken *= 8;
            return bytesTaken;
        }

        public static long EstimateImageSourceBytesUsed(this ImageSource vm)
        {
            return EstimateImageSourceBytesUsed(vm, vm.Width, vm.Height);
        }

        public static long EstimateImageSourceBytesUsed(this ImageSource vm, double width, double height)
        {
            if (width == 0)
            {
                width = vm.Height;
            }
            if (height == 0)
            {
                height = vm.Height;
            }
            return (long)Math.Ceiling((height * width) * 4D);
        }
    }
}
